package org.apache.james.mime4j.message;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.james.mime4j.decoder.CodecUtil;
import org.apache.james.mime4j.message.storage.TempFile;
import org.apache.james.mime4j.message.storage.TempPath;
import org.apache.james.mime4j.message.storage.TempStorage;
import org.apache.james.mime4j.util.CharsetUtil;

class TempFileTextBody extends AbstractBody
  implements TextBody
{
  static Class class$org$apache$james$mime4j$message$TempFileTextBody;
  private static Log log;
  private String mimeCharset = null;
  private TempFile tempFile = null;

  static
  {
    Class localClass;
    if (class$org$apache$james$mime4j$message$TempFileTextBody == null)
    {
      localClass = class$("org.apache.james.mime4j.message.TempFileTextBody");
      class$org$apache$james$mime4j$message$TempFileTextBody = localClass;
    }
    while (true)
    {
      log = LogFactory.getLog(localClass);
      return;
      localClass = class$org$apache$james$mime4j$message$TempFileTextBody;
    }
  }

  public TempFileTextBody(InputStream paramInputStream, String paramString)
    throws IOException
  {
    this.mimeCharset = paramString;
    this.tempFile = TempStorage.getInstance().getRootTempPath().createTempFile("attachment", ".txt");
    OutputStream localOutputStream = this.tempFile.getOutputStream();
    CodecUtil.copy(paramInputStream, localOutputStream);
    localOutputStream.close();
  }

  static Class class$(String paramString)
  {
    Class localClass;
    try
    {
      localClass = Class.forName(paramString);
      return localClass;
    }
    catch (ClassNotFoundException localClassNotFoundException)
    {
      throw new NoClassDefFoundError().initCause(localClassNotFoundException);
    }
  }

  public void dispose()
  {
    if (this.tempFile != null)
    {
      this.tempFile.delete();
      this.tempFile = null;
    }
  }

  public Reader getReader()
    throws UnsupportedEncodingException, IOException
  {
    String str = null;
    if (this.mimeCharset != null)
      str = CharsetUtil.toJavaCharset(this.mimeCharset);
    if (str == null)
    {
      str = "ISO-8859-1";
      if (log.isWarnEnabled())
      {
        if (this.mimeCharset != null)
          break label92;
        log.warn("No MIME charset specified. Using " + str + " instead.");
      }
    }
    while (true)
    {
      return new InputStreamReader(this.tempFile.getInputStream(), str);
      label92: log.warn("MIME charset '" + this.mimeCharset + "' has no " + "corresponding Java charset. Using " + str + " instead.");
    }
  }

  public void writeTo(OutputStream paramOutputStream, int paramInt)
    throws IOException
  {
    CodecUtil.copy(this.tempFile.getInputStream(), paramOutputStream);
  }
}